//
//  ZTAirMonitoringTopCell.m
//  YSSmartHome
//
//  Created by yanghao on 2017/4/5.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "ZTAirMonitoringTopCell.h"
#import "WaveView.h"
#import "AirAttributedView.h"
#import "ZTAirRecordButton.h"
#import "YSCommon.h"

@interface ZTAirMonitoringTopCell ()
<
CAAnimationDelegate
>
@property (nullable, nonatomic, strong) UIImageView* mImgCircle;
@property (nullable, nonatomic, strong) WaveView* wave;
@property (nullable, nonatomic, strong) AirAttributedView* mAirAttributedView;
@property (nullable, nonatomic, strong) ZTAirRecordButton* mBtnRecord;
@end
@implementation ZTAirMonitoringTopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor blueColor];
        [self initSubViews];
    }
    return self;
}


- (void)initSubViews
{
    [self.contentView addSubview:self.mImgCircle];
    [self.contentView addSubview:self.wave];
    [self.contentView addSubview:self.mAirAttributedView];
    [self.contentView addSubview:self.mBtnRecord];
    [self layoutPageSubViews];
    [self startCircleRotation:self.mImgCircle];
}

- (void)layoutPageSubViews
{
    [self.mImgCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(kIphoneSize_Height(30));
        make.centerX.equalTo(self.contentView);
        make.width.height.equalTo(kIphoneSize_Widith(248));
    }];
    [self.wave mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.mImgCircle);
        make.width.height.equalTo(kIphoneSize_Widith(208));
    }];
    [self.mAirAttributedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mImgCircle.bottom).offset(kIphoneSize_Height(50));
        make.leading.trailing.equalTo(self.contentView);
        make.height.equalTo(kIphoneSize_Height(219));
    }];
    [self.mBtnRecord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.contentView);
        make.height.equalTo(kIphoneSize_Height(53));
    }];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - event response
- (void)recordBtnTapped
{
//    if (self.delegate && [self.delegate respondsToSelector:@selector(airMonitoringTopCellRecordTapped:)])
//    {
//        [self.delegate airMonitoringTopCellRecordTapped:self];
//    }
    [self startCircleRotation:self.mImgCircle];
}
- (void)startCircleAnimation
{
    [self startCircleRotation:self.mImgCircle];
}
#pragma mark private method

- (void)startCircleRotation:(UIView*)view
{
    CABasicAnimation* rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnimation.repeatCount = MAXFLOAT;
    rotateAnimation.toValue = @(-2*M_PI) ;
    rotateAnimation.duration = 2.5;
    rotateAnimation.cumulative = YES;
    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    rotateAnimation.removedOnCompletion = NO;
    rotateAnimation.fillMode = kCAFillModeForwards;
    rotateAnimation.delegate = self;
    [view.layer addAnimation:rotateAnimation forKey:@"rotateAnimation"];
}
- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"animationDidStart");
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"animationDidStop");
}

#pragma mark getters and setters

- (void)setDevice:(ZTAirMonitorDevice *)device
{
    _device = device;
    self.mAirAttributedView.device = device;
    
}
- (UIImageView *)mImgCircle
{
    if (!_mImgCircle) {
        _mImgCircle = [[UIImageView alloc]init];
        _mImgCircle.image = [UIImage imageNamed:@"新设备-空气检测器1"];
    }
    return _mImgCircle;
}

- (AirAttributedView *)mAirAttributedView
{
    if (!_mAirAttributedView) {
        _mAirAttributedView = [[AirAttributedView alloc]init];
    }
    return _mAirAttributedView;
}

- (ZTAirRecordButton *)mBtnRecord
{
    if (!_mBtnRecord) {
        _mBtnRecord = [[ZTAirRecordButton alloc]initWithTitle:@"检测记录" imageName:@"新设备-空气检测器3-上滑"];
        [_mBtnRecord addTarget:self action:@selector(recordBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mBtnRecord;

}
- (WaveView *)wave
{
    if (!_wave) {
        _wave = [[WaveView alloc]init];
        _wave.progress = 0.5;
    }
    return _wave;
}
@end
